import store from "../store";
import axios from 'axios'
import qs from 'qs'
import utils from '@js/utils.js' /* 共用的方法 */

axios.defaults.timeout = 10000;//  请求过期时间

// http request 拦截器
axios.interceptors.request.use((config) =>{

    return config;
},(error) =>{
    return Promise.reject(err);
});

// http response 拦截器
axios.interceptors.response.use((response) => {

    return response;

},(error) => {
    if (error && error.response) {
        switch (error.response.status) {
            case 400:
                error.response.statusText = '错误请求'
                break;
            case 401:
                error.response.statusText = '未授权，请重新登录'
                break;
            case 403:
                error.response.statusText = '拒绝访问'
                break;
            case 404:
                error.response.statusText = '请求错误,未找到该资源'
                break;
            case 405:
                error.response.statusText = '请求方法未允许'
                break;
            case 408:
                error.response.statusText = '请求超时'
                break;
            case 500:
                error.response.statusText = '服务器端出错'
                break;
            case 501:
                error.response.statusText = '网络未实现'
                break;
            case 502:
                error.response.statusText = '网络错误'
                break;
            case 503:
                error.response.statusText = '服务不可用'
                break;
            case 504:
                error.response.statusText = '网络超时'
                break;
            case 505:
                error.response.statusText = 'http版本不支持该请求'
                break;
            default:
                error.response.statusText = `连接错误${error.response.status}`
        }
    } else {
        utils.mobileOrdinary("连接到服务器失败");
    }
    utils.mobileOrdinary(error.response.statusText)
    return Promise.reject(error.response)

});

/**
 * 封装axios请求
 * @param data 例如：{url: baseURL+"/api",method: 'post',data:{"typeCode": "01"}}
 *         page 默认 true  分页请求传入 false
 * @returns {Promise}
 */

export function http(data = {},page=true){
    if(page){store.commit('setLoadingStatus',true);}
   if(!data.headers){//不是json时执行
      data.data=qs.stringify(data.data);
    }
    return new Promise((resolve,reject) => {
        axios(data).then((response)=>{

          if(page){store.commit('setLoadingStatus',false);}

          //统一返回的数据格式
          if(!response.data.hasOwnProperty("respCode")){
            response.data={"returnCode":"0","respCode":"0","respMsg":"成功","returnMsg":"成功","data":response.data};
          }

          if (response.data.respCode === "0"){

              resolve(response.data);
          }else{

              //如果是业务错误返回如下
              /*if (response.data.respCode === "10"){
                  resolve(response.data);
                  return false;
              }*/

              utils.mobileOrdinary(response.data.msg);
          }
        }).catch((error)=>{
          if(page){store.commit('setLoadingStatus',false);}
          reject(error)
        });
    })
}
/**
 * 请求方式 application/json
 * @param data 例如 {url: baseURL+"/api", method: 'post', data:{"data":data,"method":"hxk.bdg.check.getQuestionnaires"}}
 * @returns {Promise}
 */

export function httpJson(data = {},page){

       var request=data,params=request.data.data,method=request.data.method;
       if(method){
          /* var config={
               "method":method,       //[string]	hxk.bdg.check.getQuestionnaires	查看
               "params":JSON.stringify(params),
               "token":(process.env.NODE_ENV == 'development') ? window.sessionStorage.getItem("token") : token,       //[string]	eyJhbGciOiJIUzUxMiJ9	查看
               "timestamp":utils.CurTime(),       //[long]	1480556543	查看
               "version":"1.0",       //[string]	1.0	查看
               "signType":2,       //[int]	1	查看
               "nonce":Math.random().toString().slice(-6),       //[string]	43f34f33	查看
               "format":1,       //[int]	1	查看
               "sign":"",       //[string]	6B9CB5489E6058FCF1B0DD1CE638D4AC
           };
           let s = config.method + config.params + config.token + config.timestamp + config.nonce;
           config.sign = md5(s).toUpperCase();
           request.data = config;*/

       }
    request.headers={"Content-Type":"application/json;charset=utf-8"} //application/json;charset=utf-8  application/x-www-form-urlencoded;charset=utf-8
    return http(request,page);
}


